# set up environment ----
library(data.table)
load("data/analysis_data.RData")

summarize <- function(v, x) {
  data.table(
    variable = v,
    mean = mean(x),
    sd = sd(x),
    min = min(x),
    max = max(x))
}
print_rows <- function(x, digits) {
  for (i in 1:nrow(x)) {
    for (k in 1:ncol(x)) {
      fmt <- paste0("%.0", digits[k], "f")
      if (k == 1) {
        cat(x[i, k, with = FALSE][[1]])
      } else {
        cat(" $", sprintf(fmt, x[i, k, with = FALSE][[1]]), "$ ", sep = "")
      }
      if (k == ncol(x)) {
        cat("\\\\\n")
      } else {
        cat(" & ", sep = "")
      }
    }
  }
}

X <- respondent_data[, rbind(
  summarize("Age 18-24", as.numeric(age_category == "18-24")),
  summarize("Age 25-34", as.numeric(age_category == "25-34")),
  summarize("Age 35-44", as.numeric(age_category == "35-44")),
  summarize("Age 45-64", as.numeric(age_category == "45-64")),
  summarize("Age Over 65", as.numeric(age_category == "Over 65")),
  summarize("Did not Vote/Supp. Other Cand. in 2016", as.numeric(support_2016 == "other/no one")),
  summarize("Supported Clinton in 2016", as.numeric(support_2016 == "Clinton")),
  summarize("Supported Sanders in 2016", as.numeric(support_2016 == "Sanders")),
  summarize("Supported Trump in 2016", as.numeric(support_2016 == "Trump")),
  summarize("HS or Less", as.numeric(education == "HS or Less")),
  summarize("Some College", as.numeric(education == "Some College")),
  summarize("College Graduate", as.numeric(education == "College Grad.")),
  summarize("Black", as.numeric(race_ethnicity == "Black")),
  summarize("Latino", as.numeric(race_ethnicity == "Latino")),
  summarize("Other/Mixed Race/Ethnicity", as.numeric(race_ethnicity == "Other/Mixed")),
  summarize("White", as.numeric(race_ethnicity == "White")),
  summarize("Female", female),
  summarize("Campaign Knowledge", campaign_knowledge),
  summarize("Follows Politics", follows_politics),
  summarize("Strong Democrat", strong_democrat),
  summarize("Trump Least Preferred", trump_least_preferred),
  summarize("Intend to Vote", vote_2020),
  summarize("Registered Voter", registered_voter),
  summarize("Ideology = ideology", ideology),
  summarize("Political Interest", political_interest),
  summarize("# Failed Attention Checks", n_attention_fails),
  summarize("Need for Cognition", need_for_cognition))]
print_rows(X, c(0, 2, 2, 1, 1))

# descriptive covariates (main)----

X <- respondent_data[n_attention_fails >= 1 &
    trump_least_preferred == 1 &
    support_2016 != "Trump", rbind(
  summarize("Age 18-24", as.numeric(age_category == "18-24")),
  summarize("Age 25-34", as.numeric(age_category == "25-34")),
  summarize("Age 35-44", as.numeric(age_category == "35-44")),
  summarize("Age 45-64", as.numeric(age_category == "45-64")),
  summarize("Age Over 65", as.numeric(age_category == "Over 65")),
  summarize("Did not Vote/Supp. Other Cand. in 2016", as.numeric(support_2016 == "other/no one")),
  summarize("Supported Clinton in 2016", as.numeric(support_2016 == "Clinton")),
  summarize("Supported Sanders in 2016", as.numeric(support_2016 == "Sanders")),
  summarize("Supported Trump in 2016", as.numeric(support_2016 == "Trump")),
  summarize("HS or Less", as.numeric(education == "HS or Less")),
  summarize("Some College", as.numeric(education == "Some College")),
  summarize("College Graduate", as.numeric(education == "College Grad.")),
  summarize("Black", as.numeric(race_ethnicity == "Black")),
  summarize("Latino", as.numeric(race_ethnicity == "Latino")),
  summarize("Other/Mixed Race/Ethnicity", as.numeric(race_ethnicity == "Other/Mixed")),
  summarize("White", as.numeric(race_ethnicity == "White")),
  summarize("Female", female),
  summarize("Campaign Knowledge", campaign_knowledge),
  summarize("Follows Politics", follows_politics),
  summarize("Strong Democrat", strong_democrat),
  summarize("Trump Least Preferred", trump_least_preferred),
  summarize("Intend to Vote", vote_2020),
  summarize("Registered Voter", registered_voter),
  summarize("Ideology = ideology", ideology),
  summarize("Political Interest", political_interest),
  summarize("# Failed Attention Checks", n_attention_fails),
  summarize("Need for Cognition", need_for_cognition))]
print_rows(X, c(0, 2, 2, 1, 1))

# candidate evaluations (all) ----
summarize <- function(v, x) {
  data.table(
    variable = v,
    mean = mean(x),
    sd = sd(x))
}
X <- merge(
  respondent_data[, rbind(
    summarize("Biden", preferences_biden),
    summarize("Bloomberg", preferences_bloomberg),
    summarize("Buttigieg", preferences_buttigieg),
    summarize("Klobuchar", preferences_klobuchar),
    summarize("Sanders", preferences_sanders),
    summarize("Warren", preferences_warren),
    summarize("Yang", preferences_yang),
    summarize("Trump", preferences_trump))],
  respondent_data[, rbind(
    summarize("Biden", electability_biden),
    summarize("Bloomberg", electability_bloomberg),
    summarize("Buttigieg", electability_buttigieg),
    summarize("Klobuchar", electability_klobuchar),
    summarize("Sanders", electability_sanders),
    summarize("Warren", electability_warren),
    summarize("Yang", electability_yang))],
  by = "variable", all.x = TRUE, sort = FALSE)
X <- merge(
  X,
  respondent_data[, rbind(
    summarize("Biden", ranking_biden),
    summarize("Bloomberg", ranking_bloomberg),
    summarize("Buttigieg", ranking_buttigieg),
    summarize("Klobuchar", ranking_klobuchar),
    summarize("Sanders", ranking_sanders),
    summarize("Warren", ranking_warren),
    summarize("Yang", ranking_yang))],
  by = "variable", all.x = TRUE, sort = FALSE)

setnames(X, c("Candidate", "Mean", "SD", "Mean", "SD", "Mean", "SD"))

print_rows(X, c(0, 0, 0, 0, 0, 1, 1))

# candidate evaluations (main) ----
X <- merge(
  respondent_data[n_attention_fails >= 1 &
      trump_least_preferred == 1 &
      support_2016 != "Trump", rbind(
    summarize("Biden", preferences_biden),
    summarize("Bloomberg", preferences_bloomberg),
    summarize("Buttigieg", preferences_buttigieg),
    summarize("Klobuchar", preferences_klobuchar),
    summarize("Sanders", preferences_sanders),
    summarize("Warren", preferences_warren),
    summarize("Yang", preferences_yang),
    summarize("Trump", preferences_trump))],
  respondent_data[, rbind(
    summarize("Biden", electability_biden),
    summarize("Bloomberg", electability_bloomberg),
    summarize("Buttigieg", electability_buttigieg),
    summarize("Klobuchar", electability_klobuchar),
    summarize("Sanders", electability_sanders),
    summarize("Warren", electability_warren),
    summarize("Yang", electability_yang))],
  by = "variable", all.x = TRUE, sort = FALSE)
X <- merge(
  X,
  respondent_data[n_attention_fails >= 1 &
      trump_least_preferred == 1 &
      support_2016 != "Trump", rbind(
    summarize("Biden", ranking_biden),
    summarize("Bloomberg", ranking_bloomberg),
    summarize("Buttigieg", ranking_buttigieg),
    summarize("Klobuchar", ranking_klobuchar),
    summarize("Sanders", ranking_sanders),
    summarize("Warren", ranking_warren),
    summarize("Yang", ranking_yang))],
  by = "variable", all.x = TRUE, sort = FALSE)

setnames(X, c("Candidate", "Mean", "SD", "Mean", "SD", "Mean", "SD"))

print_rows <- function(x, digits) {
  for (i in 1:nrow(x)) {
    for (k in 1:ncol(x)) {
      fmt <- paste0("%.0", digits[k], "f")
      if (k == 1) {
        cat(x[i, k, with = FALSE][[1]])
      } else {
        cat(" $", sprintf(fmt, x[i, k, with = FALSE][[1]]), "$ ", sep = "")
      }
      if (k == ncol(x)) {
        cat("\\\\\n")
      } else {
        cat(" & ", sep = "")
      }
    }
  }
}
print_rows(X, c(0, 0, 0, 0, 0, 1, 1))
